package com.umeox.calculator;

import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import androidx.viewpager.widget.ViewPager;

import java.util.Locale;

class EventListener implements View.OnClickListener, View.OnKeyListener, View.OnLongClickListener {
    Logic mHandler;
    ViewPager mPager;

    EventListener() {
    }

    /* access modifiers changed from: package-private */
    public void setHandler(Logic handler, ViewPager pager) {
        this.mHandler = handler;
        this.mPager = pager;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.clear:
                this.mHandler.onClear();
                return;
            case R.id.del:
                this.mHandler.onDelete();
                return;
            case R.id.equal:
                this.mHandler.onEnter();
                return;
            default:
                if (view instanceof Button) {
                    String text = ((Button) view).getText().toString();
                    if (text.length() >= 2) {
                        text = text + '(';
                    }
                    if(isNumber(text)){
                        text = String.format(Locale.getDefault(),"%d",Integer.valueOf(text));
                    }
                    this.mHandler.insert(text);
                    if (this.mPager != null && this.mPager.getCurrentItem() == 1) {
                        this.mPager.setCurrentItem(0);
                    }
                }
        }
    }

    static boolean isNumber(String text) {
        return text.length() == 1 && "0123456789".indexOf(text.charAt(0)) != -1;
    }

    public boolean onLongClick(View view) {
        if (view.getId() != R.id.del) {
            return false;
        }
        this.mHandler.onClear();
        return true;
    }

    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
        boolean z = true;
        int action = keyEvent.getAction();
        if (keyCode == 21 || keyCode == 22) {
            Logic logic = this.mHandler;
            if (keyCode != 21) {
                z = false;
            }
            return logic.eatHorizontalMove(z);
        } else if (action == 2 && keyCode == 0) {
            return true;
        } else {
            if (keyEvent.getUnicodeChar() == 61) {
                if (action != 1) {
                    return true;
                }
                this.mHandler.onEnter();
                return true;
            } else if (keyCode != 23 && keyCode != 19 && keyCode != 20 && keyCode != 66) {
                if (keyEvent.isPrintingKey() && action == 1) {
                    this.mHandler.onTextChanged();
                }
                return false;
            } else if (action != 1) {
                return true;
            } else {
                switch (keyCode) {
                    case 19:
                        this.mHandler.onUp();
                        return true;
                    case 20:
                        this.mHandler.onDown();
                        return true;
                    case 23:
                    case 66:
                        this.mHandler.onEnter();
                        return true;
                    default:
                        return true;
                }
            }
        }
    }
}
